#!/bin/bash
# $Id

# wget update_tar
# mktmpdir; unzip 
# tar list; cp from list local files; save
# untar update
# prompt for new screen

. /etc/scripts/env
. /etc/scripts/common.fn

tmpdowndir="$(mktemp -d /tmp/tmp.XXXXXX)"
tmptestdir="$(mktemp -d /tmp/tmp.XXXXXX)"
source_addr="ftp://ftp.nemonico.org"
update_file="/updates/installer/${nemonico_version}/installer_update.tar.gz"
date="$(date "+%s")"

set -eET
set -o pipefail

if [ -s $tmpdowndir/installer_update.tar.gz ]; then
	_warn_msg "Deleting previous file download ..."
	rm $tmpdowndir/installer_update.tar.gz
fi

_msg "Downloading installer updates... timeout 10 seconds."
wget -U "nemonico-installer" -P $tmpdowndir \
	-T 10  ${source_addr}/${update_file}

if [ ! -s $tmpdowndir/installer_update.tar.gz ]; then
	_err_msg "Could not connect/download update file, exiting..."
fi

cd $tmptestdir
tar tf $tmpdowndir/installer_update.tar.gz > $tmptestdir/update_list

for f in $(<${tmptestdir}/update_list) ; do
	if [ -f /${f} -o -d /${f} ]; then
		echo $f >> ${tmptestdir}/update_list.n
		test -d /${f} && sed -e '$d' -i ${tmptestdir}/update_list.n
	fi
done

_msg "\nSaving orginal files ...\n"
cd /
tar -m -T ${tmptestdir}/update_list.n \
	-czf /var/tmp/installer_original_${date}.tar.gz

_msg "\nPerforming update ...\n"
cd /
tar -m -vxf $tmpdowndir/installer_update.tar.gz

_msg "\n\nNow open another screen (Ctrl-a c).\n"


# vim: ts=2
# installer_update.sh: end
